package cn.hpclub.server.controller.h5;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.constant.H5ApiConstant;
import cn.hpclub.server.model.SysCodeType;
import cn.hpclub.server.service.MemberOrgService;
import cn.hpclub.server.service.PaymentService;
import cn.hpclub.server.service.SysCodeTypeService;
import cn.hpclub.server.util.InterfaceUtil;

public class ConfigH5Controller extends BaseH5Controller{

    private static Logger       logger       = LoggerFactory.getLogger(ConfigH5Controller.class);
    private static final String SERVICE_TYPE = "service_type";

    public void getProfileList(){
        JSONObject jo = setResponseObj(H5ApiConstant.ResultCode.FAIL_CODE, H5ApiConstant.Msg.FAIL,
                                       H5ApiConstant.Msg.EMPTY);
        try{
            String serviceType = getPara(SERVICE_TYPE, null);
            int orgId = getParaToInt("orgId", 0);

            if(null == serviceType || "".equals(serviceType)){
                jo = setResponseObj(H5ApiConstant.ResultCode.FAIL_CODE, H5ApiConstant.Msg.PARAEXCEPTION,
                                    H5ApiConstant.Msg.EMPTY);
                renderText(jo.toString());
                return;
            }

            List<Record> list = SysCodeType.dao.getCodeValueByTypeCode(serviceType);
            JSONArray jsonArray = toJsonArray(list);

            JSONObject obj = new JSONObject();
            obj.put("items", jsonArray);

            if(orgId > 0){// 是否德阳
                obj.put("isOrgInDeYang", InterfaceUtil.isOrgInDeYang(orgId));
            }
            jo = setResponseObj(H5ApiConstant.ResultCode.SUCCESS_CODE, H5ApiConstant.Msg.SUCCESS, obj);

        }
        catch(Exception e){
            logger.error(e.getMessage());
            jo = setResponseObj(H5ApiConstant.ResultCode.EXCEPTION_CODE, e.getMessage(), H5ApiConstant.Msg.EMPTY);
        }

        renderText(jo.toString());
    }

    public void getCommonProfileList(){
        JSONObject jo = setResponseObj(H5ApiConstant.ResultCode.FAIL_CODE, H5ApiConstant.Msg.FAIL,
                                       H5ApiConstant.Msg.EMPTY);
        try{
            String serviceType = getPara(SERVICE_TYPE, null);

            if(null == serviceType || "".equals(serviceType)){
                jo = setResponseObj(H5ApiConstant.ResultCode.FAIL_CODE, H5ApiConstant.Msg.PARAEXCEPTION,
                                    H5ApiConstant.Msg.EMPTY);
                renderText(jo.toString());
                return;
            }

            List<Record> list = SysCodeType.dao.getCodeValueByTypeCode(serviceType);
            JSONArray jsonArray = toJsonArray(list);
            jo = setResponseObj(H5ApiConstant.ResultCode.SUCCESS_CODE, H5ApiConstant.Msg.SUCCESS, jsonArray);

        }
        catch(Exception e){
            logger.error(e.getMessage());
            jo = setResponseObj(H5ApiConstant.ResultCode.EXCEPTION_CODE, e.getMessage(), H5ApiConstant.Msg.EMPTY);
        }

        renderText(jo.toString());
    }

    /**
     * 
     * @Title: getProfileListByUserInfo
     * @Description:根据人员所属地市渠道编码获取政企客户经理能够访问的数据（用于大掌柜-客户端-政企专区首页-德阳终端销售H5页面入口）
     */
    public void getProfileListByUserInfo(){
        JSONObject jo = setResponseObj(H5ApiConstant.ResultCode.FAIL_CODE, H5ApiConstant.Msg.FAIL,
                                       H5ApiConstant.Msg.EMPTY);
        try{
            String serviceType = getPara(SERVICE_TYPE, null);
            String userId = getPara("userId", null);
            String groupId = getPara("groupId", null);// 当前用户所属地市分公司渠道编码，如德阳为17

            if(null == serviceType || "".equals(serviceType) || null == userId || "".equals(userId) || null == groupId
                    || "".equals(groupId)){
                jo = setResponseObj(H5ApiConstant.ResultCode.FAIL_CODE, H5ApiConstant.Msg.PARAEXCEPTION,
                                    H5ApiConstant.Msg.EMPTY);
                renderText(jo.toString());
                return;
            }

            List<Record> list = null;
            // 限制为只有政企客户经理才能访问政企专区相关的数据，由于主题模块已经限制了只有政企客户经理才能看见政企专区首页入口，故此处不需要再通过开关控制
            boolean isAccountsManager = MemberOrgService.service.isAccountsManager(userId);
            if(isAccountsManager){// 如果是政企客户经理，根据地市渠道编码取数据
                list = SysCodeTypeService.service.getProfileListByGroupId(serviceType, groupId);
            }

            JSONArray jsonArray = new JSONArray();
            if(null != list){
                jsonArray = toJsonArray(list);
            }

            jo = setResponseObj(H5ApiConstant.ResultCode.SUCCESS_CODE, H5ApiConstant.Msg.SUCCESS, jsonArray);
        }
        catch(Exception e){
            logger.error(e.getMessage());
            jo = setResponseObj(H5ApiConstant.ResultCode.EXCEPTION_CODE, e.getMessage(), H5ApiConstant.Msg.EMPTY);
        }

        renderText(jo.toString());
    }

    private JSONArray toJsonArray(List<Record> list){
        JSONArray jsonArray = new JSONArray();
        for(int i = 0; i < list.size(); i++){
            JSONObject json = new JSONObject();
            Record item = list.get(i);
            json.put("code", (null == item.getStr("code")) ? "" : item.getStr("code"));
            json.put("name", (null == item.getStr("name")) ? "" : item.getStr("name"));
            json.put("value", (null == item.getStr("value")) ? "" : item.getStr("value"));
            json.put("remark", (null == item.getStr("remark")) ? "" : item.getStr("remark"));
            json.put("sort", (null == item.getInt("sort")) ? 0 : item.getInt("sort"));

            jsonArray.put(json);
        }
        return jsonArray;
    }

    /**
     * @Title: getPaymentList
     * @Description:获取支付列表接口 void
     */
    public void getPaymentList(){
        try{
            int userId = getParaToInt("userId");
            int moduleId = getParaToInt("moduleId");
            JSONObject result = PaymentService.getInstance().getPaymentList(userId, moduleId);
            renderText(result.toString());
        }
        catch(NullPointerException e){
            e.printStackTrace();
            JSONObject jo = setResponseObj(H5ApiConstant.ResultCode.FAIL_CODE, H5ApiConstant.Msg.PARA_ILLEGAL,
                                           H5ApiConstant.Msg.EMPTY);
            renderText(jo.toString());
        }
    }
}
